package com.ycy;

import org.flowable.bpmn.model.*;
import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.task.api.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class BPDemo {

  ProcessEngine processEngine;

  ProcessEngineConfiguration cfg;

  @Before
  public void before() {
    cfg = new StandaloneProcessEngineConfiguration()
//                .setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
//                .setJdbcDriver("org.h2.Driver")
//                .setJdbcUsername("sa")
            .setJdbcPassword("")
            .setJdbcUrl("jdbc:mysql://10.211.55.3:3306/flowablebp?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8")
            .setJdbcUsername("root")
            .setJdbcPassword("123456")
            .setJdbcDriver("com.mysql.jdbc.Driver")

            // 如果数据表不存在的时候，自动创建数据表
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

    // 执行完成后，就可以开始创建我们的流程了
    processEngine = cfg.buildProcessEngine();
  }

  /**
   * 审核通过
   */
  @Test
  public void listener1() {
    ProcessEngine processEngine = this.cfg.buildProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
    System.out.println("You have " + tasks.size() + " tasks:");

    for (int i = 0; i < tasks.size(); i++) {
      Task task = tasks.get(i);
      String id = task.getId();

      //打印task任务信息
      System.out.println((i + 1) + ") " + id + " : " + task.getName() + " : " + task.getProcessInstanceId()
      );

      System.out.println(task.getTaskLocalVariables());
      System.out.println(task.getProcessVariables());
      //只有通过这个 api 才能获取 task 的参数
      System.out.println(taskService.getVariables(task.getId()));

    }

  }

  //获取下一个节点的信息
  @Test
  public void queryVars() {
    TaskService taskService = processEngine.getTaskService();
    Task task = taskService.createTaskQuery().processInstanceId("70001").singleResult();

    RepositoryService repositoryService = processEngine.getRepositoryService();

    RuntimeService runtimeService = processEngine.getRuntimeService();

    //获取流程发布Id信息
    String definitionId = runtimeService.createProcessInstanceQuery().processInstanceId("70001")
            .singleResult().getProcessDefinitionId();


    //获取bpm对象
    BpmnModel bpmnModel = repositoryService.getBpmnModel(definitionId);

    //传节点定义key 获取当前节点
    String taskDefinitionKey = task.getTaskDefinitionKey();
    FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(taskDefinitionKey);

    //输出连线
    List<SequenceFlow> outgoingFlows = flowNode.getOutgoingFlows();

    //遍历返回下一个节点信息
    for (SequenceFlow outgoingFlow : outgoingFlows) {
      //类型自己判断
      FlowElement targetFlowElement = outgoingFlow.getTargetFlowElement();
      //用户任务
      if (targetFlowElement instanceof UserTask) {
        //。。。
      } else if (targetFlowElement instanceof ExclusiveGateway) {
        setExclusiveGateway(targetFlowElement);
      }
    }
  }

  private void setExclusiveGateway(FlowElement targetFlow) {
    //排他网关，获取连线信息
    List<SequenceFlow> targetFlows = ((ExclusiveGateway) targetFlow).getOutgoingFlows();
    for (SequenceFlow sequenceFlow : targetFlows) {
      //目标节点信息
      FlowElement targetFlowElement = sequenceFlow.getTargetFlowElement();
      if (targetFlowElement instanceof UserTask) {
        // do something
      } else if (targetFlowElement instanceof EndEvent) {
        // do something
      } else if (targetFlowElement instanceof ServiceTask) {
        // do something
      } else if (targetFlowElement instanceof ExclusiveGateway) {
        //递归寻找
        setExclusiveGateway(targetFlowElement);
      } else if (targetFlowElement instanceof SubProcess) {
        // do something
      }
    }
  }




}
